﻿using System;
using System.Collections.Generic;
using System.Text;

namespace T.FAS.Runtime.DataAccess.CDOEngine
{
    /// <summary>
    /// Select语句分组声明
    /// </summary>
    /// <remarks>类的补充说明</remarks>
    [Serializable]
    public class HavingStatement : SqlElement
    {
        #region 常量

        /// <summary> 
        /// SelectListStatement 
        /// </summary>
        public const string SELECTLISTSTATEMENT = "HavingStatement";

        #endregion       

        #region 构造函数

        /// <summary>
        /// 构造函数
        /// </summary>
        public HavingStatement()
        {
            base.CreateChildCollection();
        }

        #endregion       

        #region 方法

        /// <summary>
        /// 将聚合元素转换成SQL，只能用作Select {columns} 的{columns}部分
        /// </summary>
        /// <returns>SQL</returns>
        public override string ToSQL()
        {
            StringBuilder result = new StringBuilder(" (");
            for (int index = 0; index < this.ChildCollection.Count; index++)
            {
                result.Append(this.ChildCollection[index].ToSQL());
                if (index < this.ChildCollection.Count - 1)
                    result.Append(" AND ");
            }
            result.Append(") ");
            return result.ToString();
        }
        #endregion
    }

    /// <summary>
    /// Select语句分组声明
    /// </summary>
    /// <remarks>类的补充说明</remarks>
    [Serializable]
    public class HavingItem : SqlElement
    {
        #region 常量

        public const string SELECTLISTSTATEMENT = "HavingItem";

        #endregion       

        #region 构造函数

        /// <summary>
        /// 构造函数
        /// </summary>
        public HavingItem()
        {
            base.CreateChildCollection();
        }

        #endregion

        #region 属性

        /// <summary>
        /// having 子项。例：count(id) > 0
        /// </summary>
        public string ExpressionString { get; set; }
        #endregion

        #region 方法

        /// <summary>
        /// 将聚合元素转换成SQL，只能用作Select {columns} 的{columns}部分
        /// </summary>
        /// <returns>SQL</returns>
        public override string ToSQL()
        {
            return ExpressionString;
        }
        #endregion
    }
}
